
<div class="content-body">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 工具栏 -->
            <div class="layui-btn-group">
                <button class="layui-btn" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加等级</button>
            </div>

            <!-- 数据表格 -->
            <table id="LAY-level-list" lay-filter="LAY-level-list"></table>
        </div>
    </div>
</div>

<!-- 表格操作列模板 -->
<script type="text/html" id="table-level-operation">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>

<script>
layui.use(['table', 'form', 'layer'], function(){
    var $ = layui.$
    ,table = layui.table
    ,form = layui.form
    ,layer = layui.layer;
    
    //会员等级列表
    table.render({
        elem: '#LAY-level-list'
        ,url: '/app/ky_shouyin/admin/member/levelList'
        ,cols: [[
            {type: 'numbers', fixed: 'left'}
            ,{field: 'name', title: '等级名称', width: 120}
            ,{field: 'discount', title: '折扣率', width: 100, templet: function(d){
                return d.discount + '%';
            }}
            ,{field: 'points_rate', title: '积分比例', width: 100, templet: function(d){
                return d.points_rate + '%';
            }}
            ,{field: 'upgrade_points', title: '升级积分', width: 120}
            ,{field: 'status', title: '状态', width: 100, templet: function(d){
                return d.status == 1 ? 
                    '<span class="layui-badge layui-bg-green">启用</span>' : 
                    '<span class="layui-badge layui-bg-gray">禁用</span>';
            }}
            ,{field: 'created_at', title: '创建时间', width: 160}
            ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-level-operation'}
        ]]
        ,page: true
        ,limit: 15
        ,text: {
            none: '暂无数据'
        }
    });
    
    //监听工具条
    table.on('tool(LAY-level-list)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit'){
            layer.open({
                type: 2
                ,title: '编辑等级'
                ,content: '/app/ky_shouyin/admin/member/levelEdit?id=' + data.id
                ,area: ['500px', '400px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submitID = 'LAY-level-submit'
                    ,submit = layero.find('iframe').contents().find('#'+ submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        
                        //提交修改
                        $.ajax({
                            url: '/app/ky_shouyin/admin/member/levelEdit'
                            ,type: 'POST'
                            ,data: field
                            ,success: function(res){
                                if(res.code === 0){
                                    layer.msg('修改成功', {icon: 1});
                                    table.reload('LAY-level-list');
                                    layer.close(index);
                                } else {
                                    layer.msg(res.msg || '修改失败', {icon: 2});
                                }
                            }
                        });
                    });
                    
                    submit.trigger('click');
                }
            });
        } else if(obj.event === 'del'){
            layer.confirm('确定删除该等级？', function(index){
                $.ajax({
                    url: '/app/ky_shouyin/admin/member/levelDelete'
                    ,type: 'POST'
                    ,data: {id: data.id}
                    ,success: function(res){
                        if(res.code === 0){
                            layer.msg('删除成功', {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(res.msg || '删除失败', {icon: 2});
                        }
                    }
                });
                layer.close(index);
            });
        }
    });

    var active = {
        add: function(){
            layer.open({
                type: 2
                ,title: '添加等级'
                ,content: '/app/ky_shouyin/admin/member/levelAdd'
                ,area: ['500px', '400px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submitID = 'LAY-level-submit'
                    ,submit = layero.find('iframe').contents().find('#'+ submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        
                        //提交新增
                        $.ajax({
                            url: '/app/ky_shouyin/admin/member/levelAdd'
                            ,type: 'POST'
                            ,data: field
                            ,success: function(res){
                                if(res.code === 0){
                                    layer.msg('添加成功', {icon: 1});
                                    table.reload('LAY-level-list');
                                    layer.close(index);
                                } else {
                                    layer.msg(res.msg || '添加失败', {icon: 2});
                                }
                            }
                        });
                    });
                    
                    submit.trigger('click');
                }
            }); 
        }
    };
    
    $('.layui-btn-group .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});
</script> 